package cn.com.cetccst.mdm;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

import cn.com.cetccst.mdm.engine.BaseMDMEngine;
import cn.com.cetccst.mdm.engine.DQTMdmEngine;
import cn.com.cetccst.mdm.engine.HWMdmEngine;
import cn.com.cetccst.mdm.engine.HonorMdmEngine;
import cn.com.cetccst.mdm.engine.IMDMEngine;
import cn.com.cetccst.mdm.engine.LenovoMdmEngine;
import cn.com.cetccst.mdm.engine.MockMDMEngine;
import cn.com.cetccst.mdm.engine.OPPOMdmEngine;
import cn.com.cetccst.mdm.engine.TdMdmEngine;
import cn.com.cetccst.mdm.engine.XiaomiMdmEngine;

public class MDMManager {
    public enum TYPE {
        Mock, BYOD, Huawei_DGT, Huawei_DQT, TD, Huawei_HEM, Honor, Oppo, Lenovo, Xiaomi
    }

    private static MDMManager instance;
    private TYPE mdmType = TYPE.BYOD;
    private final IMDMEngine mdmEngine;
    private BaseMDMEngine.Callback logger;

    public static MDMManager getInstance() {
        return instance;
    }

    public static void init(Context context, ComponentName adminComponent, String fileProviderAuthority, BaseMDMEngine.Callback logger) {
        if (instance == null) {
            instance = new MDMManager(context, adminComponent, fileProviderAuthority, false, logger);
        }
    }

    public static void initMockEngine(Context context, ComponentName adminComponent, String fileProviderAuthority, BaseMDMEngine.Callback logger) {
        if (instance == null) {
            instance = new MDMManager(context, adminComponent, fileProviderAuthority, true, logger);
        }
    }

    public TYPE getMdmType() {
        return mdmType;
    }

    public String getMdmName() {
        return mdmType.name();
    }

    public IMDMEngine getMdmEngine() {
        return mdmEngine;
    }

    public BaseMDMEngine.Callback getLogger() {
        return logger;
    }

    public boolean isActiveDeviceAdmin() {
        return mdmEngine.isActiveDeviceAdmin();
    }

    private MDMManager(Context context, ComponentName adminComponent, String fileProviderAuthority, boolean isMock, BaseMDMEngine.Callback logger) {
        if (isMock) {
            mdmType = TYPE.Mock;
            mdmEngine = new MockMDMEngine(context);
        } else {
            initDeviceType(context, adminComponent);

            if (mdmType == TYPE.Huawei_DGT || mdmType == TYPE.Huawei_HEM) {
                mdmEngine = new HWMdmEngine(context);
            } else if (mdmType == TYPE.Huawei_DQT) {
                mdmEngine = new DQTMdmEngine(context);
            } else if (mdmType == TYPE.TD) {
                mdmEngine = new TdMdmEngine(context);
            } else if (mdmType == TYPE.Honor) {
                mdmEngine = new HonorMdmEngine(context);
            } else if (mdmType == TYPE.Oppo) {
                mdmEngine = new OPPOMdmEngine(context);
            } else if (mdmType == TYPE.Lenovo) {
                mdmEngine = new LenovoMdmEngine(context);
            } else if (mdmType == TYPE.Xiaomi) {
                mdmEngine = new XiaomiMdmEngine(context);
            } else {
                mdmEngine = new BaseMDMEngine(context);
            }
        }
        mdmEngine.config(adminComponent, fileProviderAuthority, logger);
    }

    private void initDeviceType(Context context, ComponentName adminComponent) {
        Log.e("DeviceL", String.format("brand:%s", Build.BRAND));
        if (Build.BRAND.equalsIgnoreCase("HuaWei")) {
            String provider = getHuaweiDeviceCustomProvider();
            if (provider.equalsIgnoreCase("DGT")) {
                mdmType = TYPE.Huawei_DGT;
            } else if (provider.equalsIgnoreCase("DT")) {
                if (isDingQiao()) {
                    mdmType = TYPE.TD;
                } else {
                    mdmType = TYPE.Huawei_DQT;
                }
            } else {
                DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
                if (devicePolicyManager != null && devicePolicyManager.isAdminActive(adminComponent)) {
                    mdmType = TYPE.Huawei_HEM;
                }
            }
        } else if (Build.BRAND.equalsIgnoreCase("Honor")) {
            mdmType = TYPE.Honor;
        } else if (Build.BRAND.equalsIgnoreCase("OPPO")) {
            mdmType = TYPE.Oppo;
        } else if (Build.BRAND.equalsIgnoreCase("Lenovo")) {
            mdmType = TYPE.Lenovo;
        } else if (Build.BRAND.equalsIgnoreCase("Redmi")) {
            mdmType = TYPE.Xiaomi;
        } else {
            mdmType = TYPE.BYOD;
        }
    }

    //todo 老鼎桥配置
    private boolean isDingQiao() {
        return true;
    }

    private static String getHuaweiDeviceCustomProvider() {
        String provider = "";
        try {
            String version = Build.DISPLAY;
            int index = version.indexOf("(");
            if (index >= 0) {
                version = version.substring(index + 1);
                if (version.startsWith("DGT")) {
                    provider = "DGT";
                } else if (version.indexOf("C700") > 0) {
                    provider = "DT";
                }
            }

        } catch (Exception e) {
        }
        return provider;
    }

    private static String getPropValue(String propName) {
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, propName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }

}
